/**
 * 切换角色触发用户事件
 */
function changeRoleUser() {
    initUserSelect();
}

/**
 * 初始化角色选择
 */
function initUserSelect() {
    $("#user-selected-option").empty();
    $.hcPostAjax({
        msgPrefix: '获取角色信息出错：',
        url: basePath + "/role/user/list",
        data: {roleid: roleTree.jstree("get_selected")[0]},
        success: function (data) {
            laytpl($("#userSelectOption").html()).render({list: data}, function (html) {
                $("#user-selected-option").append(html);
            });
            initMulitSelect();
        }
    });
}

/**
 * 初始化设备选择div
 * @param index
 */
function initMulitSelect() {
    $('#user-selected-option').multiSelects({
        selectableHeader: "<input id='searchselectable' type='text' class='search-input form-control mulitSelectInputHeader' autocomplete='off' placeholder='请输入角色名称'>" +
        "<div class='mulitSelectDivHeader'>可选用户</div>",
        selectionHeader: "<input id='searchselection' type='text' class='search-input form-control mulitSelectInputHeader' autocomplete='off' placeholder='请输入角色名称'>" +
        "<div class='mulitSelectDivHeader'>已选用户</div>",
        afterInit: function(ms){
            var that = this,
                $selectableSearch = $('input#searchselectable'),
                $selectionSearch = $('input#searchselection'),
                selectableSearchString = '#'+that.$container.attr('id')+' .ms-elem-selectable:not(.ms-selected)',
                selectionSearchString = '#'+that.$container.attr('id')+' .ms-elem-selection.ms-selected';

            that.qs1 = $selectableSearch.quicksearch(selectableSearchString)
                .on('keydown', function(e){
                    if (e.which === 40){
                        that.$selectableUl.focus();
                        return false;
                    }
                });

            that.qs2 = $selectionSearch.quicksearch(selectionSearchString)
                .on('keydown', function(e){
                    if (e.which == 40){
                        that.$selectionUl.focus();
                        return false;
                    }
                });

            var hheight = window.innerHeight - 230;
            $("#ms-node-selected-option").css("width", "100%");
            $(".ms-list").css({"height": hheight  + "px", "border-top":"0px"});
        },
        disabledClass: "disabled",
        afterSelect: function(values){
            addRoleUser(values[0]);
            this.qs1.cache();
            this.qs2.cache();
        },
        afterDeselect: function(values){
            removeRoleUser(values[0]);
            this.qs1.cache();
            this.qs2.cache();
        }
    });
}

/**
 * 保存角色用户
 * @param userid
 */
function addRoleUser(userid) {
    $.hcPostAjax({
        msgPrefix: '保存角色用户数据失败：',
        url: basePath + '/role/user/save',
        data: {roleid: roleTree.jstree("get_selected")[0], userid: userid},
        success: function () {
            layer.msg('保存成功！', {icon: 1,time:1500});
        }
    })
}

/**
 * 删除角色用户
 * @param userid
 */
function removeRoleUser(userid) {
    $.hcPostAjax({
        msgPrefix: '删除角色用户数据失败：',
        url: basePath + '/role/user/delete',
        data: {roleid: roleTree.jstree("get_selected")[0], userid: userid},
        success: function () {
            layer.msg('删除成功！', {icon: 1,time:1500});
        }
    })
}